home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / backup / taper-6.000 / taper-6 / taper-6.7.3 / bg_common.c < prev    next >
C/C++ Source or Header  |  1996-06-17  |  8KB  |  355 lines

  1. /* Common routines to background programs and main
  2.    taper program */
  3.    
  4.    
  5. #include "taper.h"   
  6.  
  7.  
  8. void mail_finish(char *prog)
  9. /* Prints a message to the mail file about completion */
  10. {
  11.     time_t t;
  12.     char s[100];
  13.     
  14.     time(&t);
  15.     sprintf(s, "\n%s finished at %s\n", prog, ctime(&t));
  16.     write(mf, s, strlen(s));
  17. }
  18.  
  19.  
  20. void sendmail()
  21. {
  22. /* Closes mail file, sends mail and deletes mail file */
  23.     char s[MAX_FNAME];
  24.     
  25.     mail_finish("Backup");
  26.     close(mf);
  27.     sprintf(s, "%s %s -s \"Taper Unattended Backup\" < %s", MAIL_PROG, MAIL_TO, mailfn);   /* send mail */
  28.     system(s);
  29.     unlink(mailfn);
  30. }
  31.  
  32.  
  33. _errstat do_exit(int c) {
  34.     char err[100];
  35.     _s32  adjc=0;
  36.     
  37.     while (errs[adjc].err_no)             /* find error in template */
  38.       if (errs[adjc].err_no == c)
  39.         break;
  40.       else adjc++;
  41.     strcpy(err, strerror(errno));
  42.     strcat(err, " while ");
  43.     (errs[adjc].no_append) ? strcpy(err, errs[adjc].mess) :
  44.     (errs[adjc].err_no == 0) ? strcat(err, "Unknown error") :
  45.                                strcat(err, errs[adjc].mess);
  46.     write_log(err);
  47.     (win_main == NULL) ? fprintf(stderr, "%s\n", err) :
  48.                          message_box(err, MB_OK);
  49.     if (errs[adjc].exit || !c) {         /* need to properly exit */
  50.         write_log("Exiting with error");
  51.         if (lf) 
  52.        write(lf, "\n\n", strlen("\n\n"));
  53.     my_free_all();                 /* free all user memory */
  54.     exit(-1);
  55.     }
  56.     log_errors++;
  57.     if (mf) {                      /* write to mail file */
  58.     strcat(err, "\n");
  59.     write(mf, err, strlen(err));
  60.     strcpy(err, "Backup ABORTED\n");
  61.     write(mf, err, strlen(err));
  62.     sendmail();                 /* send mail */
  63.     }
  64.     return -1;
  65. }
  66.  
  67.  
  68. _errstat exclude_list(char *fn, char *list)
  69. {
  70. /* Determines whether to exclude file 'fn' from archive 
  71.  * 
  72.  * returns 1 if should be excluded (ie. in list)
  73.  * returns 0 if shouldn't be excluded
  74. */
  75.     char *s, *b;
  76.     char tok[MAX_FNAME];
  77.     int fin=0;
  78.    
  79.     if (!*list) return 0;                         /* empty exclusion list */ 
  80.     strcpy(tok, list);
  81.     s=tok;
  82.     while (!fin) {
  83.     b=s;
  84.     while (*s && (*s != ' ')) s++;         /* look for space or eos */
  85.     if (!*s) fin=1;                 /* end of string */
  86.     *s = 0; 
  87.     if (!strcasecmp(b, &fn[strlen(fn)-strlen(b)]))
  88.       return 1;
  89.     s++;
  90.     }
  91.     return 0;
  92. }
  93.  
  94.  
  95. _errstat exclude_compression(char *fn)
  96. {
  97. /* Checks to see if file is one of the files not to compress.
  98.  * 
  99.  * returns 1 if should be excluded (ie. in list)
  100.  * returns 0 if shouldn't be excluded
  101. */
  102.     return exclude_list(fn, exclude_compress);
  103. }
  104.  
  105.     
  106. _errstat malloc_comp_buffers()
  107. {
  108.     ULONG pci;
  109.     struct compress_identity *ci;
  110.     
  111.     compress(COMPRESS_ACTION_IDENTITY, NULL, NULL, 0, NULL, &pci);   /* get compression buffer */
  112.     ci = (struct compress_identity *) pci;
  113.     cbuf = my_malloc(ci->memory);
  114.     if (cbuf == NULL) return do_exit(ERROR_MEMORY);
  115.     comp_buffer1 = my_malloc(COMPRESS2_BUFFER_SIZE);
  116.     if (comp_buffer1 == NULL) return do_exit(ERROR_MEMORY);
  117.     comp_buffer2 = my_malloc(COMPRESS2_BUFFER_SIZE);
  118.     if (comp_buffer2 == NULL) return do_exit(ERROR_MEMORY);
  119.     return 0;
  120. }
  121.  
  122.  
  123. void free_comp_buffers()
  124. {
  125.     if (comp_buffer1) my_free(comp_buffer1);
  126.     if (comp_buffer2) my_free(comp_buffer2);
  127.     if (cbuf) my_free(cbuf);
  128. }
  129.  
  130.  
  131. void write_log(char *s)
  132. {
  133.     time_t  t;
  134.     char    s1[500], s2[500];
  135.     
  136.     if (!lf) return;                 /* no log file open */
  137.     t = time(NULL);
  138.     strcpy(s1, ctime(&t));
  139.     s1[strlen(s1)-1] = 0;             /* remove trailing \n */
  140.     sprintf(s2, "%s:  %s\n", s1, s);
  141.     write(lf, s2, strlen(s2));
  142. }
  143.  
  144.  
  145. void write_error_log(char *s)
  146. {
  147. /* Writes an error to the log file. 
  148.  * 
  149.  * Format:   Date:  ERROR: Error while s
  150. */ 
  151.     char s1[5000];
  152.     
  153.     sprintf(s1, "ERROR:  %s while %s", strerror(errno), s);
  154.     write_log(s1);
  155.     log_errors++;
  156. }
  157.  
  158.  
  159. void write_warning_log(char *s)
  160. {
  161. /* Writes a warning to the log file. 
  162.  * 
  163.  * Format:   Date:  WARNING: s
  164. */ 
  165.     char s1[200];
  166.     
  167.     sprintf(s1, "WARNING:  %s", s);
  168.     log_warnings++;
  169.     write_log(s1);
  170. }
  171.  
  172.  
  173. void write_fatal_log(char *s)
  174. {
  175. /* Writes a fatal error to the log file. 
  176.  * 
  177.  * Format:   Date:  FATAL ERROR: Error while s
  178. */ 
  179.     char s1[5000];
  180.     
  181.     sprintf(s1, "FATAL ERROR:  %s while %s", strerror(errno), s);
  182.     log_errors++;
  183.     write_log(s1);
  184. }
  185.  
  186.  
  187.    
  188.  
  189. void taper_tmpnam(char *s)
  190. {
  191. /* Makes a temporary filename for use by taper.
  192.  * Simply calls tempnam and appends taper to it
  193.  */
  194.     char *x=tempnam(temp_dir, "taper");
  195.     
  196.     strcpy(s, x); 
  197.     free(x);
  198. }
  199.  
  200.  
  201. void change_dollar(char *s)
  202. {
  203. /* Reads a string an replaces $ with \$
  204.  * To avoid shell expansions
  205.  */
  206.     
  207.     char  *s1=s, *s2;
  208.     
  209.     while (*s1) {
  210.     if (*s1 == '$') {
  211.         for (s2=&s[strlen(s)]; s2>=s1; s2--)
  212.           *(s2+1) = *s2;
  213.         *s1 = '\\';
  214.         s1++;
  215.     }
  216.     s1++;
  217.     }
  218. }
  219.  
  220.  
  221. _errstat make_dirs(char *s)
  222. /* Makes directories for a filename.
  223.  * For example, if s = t/usd/y/f, it will make directories
  224.  * t, then usd then y. Permission are rwr-xr-x and the owner
  225.  * is the owner of the person running the program.
  226.  * 
  227.  * Returns 0 if error occurred, otherwise 1
  228. */ 
  229. {
  230.     char *s1;
  231.     char s2[MAX_FNAME], s3[MAX_FNAME];
  232.     int  c=0;
  233.     DIR *dir;
  234.     
  235.     strcpy(s3, s);
  236.     if(strrchr(s3, '/') == NULL)         /* filename only */
  237.       return 1;
  238.     *strrchr(s3, '/') = 0;             /* remove filename */
  239.     
  240.     *s2 = 0;
  241.     s1 = s3;
  242.     if (*s1 == '/') {                 /* we don't try and create */
  243.     strcpy(s2, "/");             /* the root directory */
  244.     s1++;
  245.     c = 1;
  246.     }
  247.     while (*s1) {
  248.     while (*s1 && (*s1 != '/')) 
  249.         s2[c++] = *s1++;
  250.     s2[c] = 0;
  251.     if (*s1) s1++;
  252.     if ((dir=opendir(s2)) == NULL)          /* check to see if dir exists */
  253.       if (errno == ENOENT) {         /* no it doesn't */
  254.           if (mkdir(s2, 493) == -1) {         /* try and create */
  255.           sprintf(s3, "creating directory %s", s2);
  256.           write_error_log(s3);
  257.           return 0;
  258.           }
  259.       }
  260.       else {
  261.           sprintf(s3, "opening directory %s", s2);
  262.           write_error_log(s3);
  263.           return 0;
  264.       }
  265.     
  266.     if (dir)                 /* close dir if it was opened */
  267.       closedir(dir);
  268.     s2[c++] = '/';
  269.     }
  270.     return 1;
  271. }
  272.     
  273.  
  274. _errstat my_rename(char *oldf, char *newf)
  275. {
  276. /* Renames filename 'old' to 'new'. New is overwritten
  277.  * 
  278.  * If it is a cross-device link, the file is copied.
  279.  * 
  280.  * The tr_buffer is used and assumed to have been initialized
  281.  * and of size max_tr_size
  282.  * 
  283.  * Returns 0 if OK, -1 otherwise
  284. */
  285.     int oldfd, newfd, e;
  286.     struct stat b;
  287.     
  288.     if (rename(oldf, newf) == 0)
  289.       return 0;
  290.     if (errno != EXDEV)                 /* couldn't rename*/
  291.       return do_exit(ERROR_RENAMING);
  292.     /* cross device - must copy file */
  293.     
  294.     oldfd = open(oldf, O_RDONLY);
  295.     if (oldfd == -1) return do_exit(ERROR_RENAMING);
  296.     fstat(oldfd, &b);
  297.     newfd = creat(newf, b.st_mode);
  298.     if (newfd == -1) {
  299.     close(oldfd);
  300.     return do_exit(ERROR_RENAMING);
  301.     }
  302.     e = my_filecopy(oldfd, newfd);
  303.     close(oldfd);
  304.     close(newfd);
  305.     (e == -1) ?  unlink(newf) : unlink(oldf);     /* if error, remove new file, else old file */
  306.     if (e == -1) return do_exit(ERROR_RENAMING);
  307.     return 0;
  308. }
  309.  
  310.  
  311. _errstat my_filecopy(int oldfd, int newfd)
  312. {
  313. /* Copies the contents of file oldf to newf.
  314.  * 
  315.  * Assumes both files are opened and positioned correctly.
  316.  * Uses tr_buffer and assumes that it is initialised and is
  317.  * of size max_tr_size
  318.  * 
  319.  * Returns 0 if OK, -1 otherwise
  320. */
  321.     _s32 x;
  322.  
  323.     while (1) {
  324.     x = read(oldfd, tr_buffer, max_tr_size);
  325.     if (!x) break;
  326.     if (x == -1)
  327.       return -1;
  328.     write(newfd, tr_buffer, x);
  329.     }                 
  330.     return 0;
  331. }
  332.  
  333.  
  334. _errstat setowners(char *s, _errstat ret, struct file_info *fi)
  335. {
  336.     char l[500];
  337.     _s32  sz;
  338.     struct utimbuf ut;
  339.  
  340.     if (!S_ISLNK(fi->mode)) {             /* don't set anything for links */
  341.     sprintf(l, "Setting permissions, modes & times for %s", s);
  342.     if (log_level > 2) write_log(l);
  343.     sz = 0;
  344.     sz += chmod(s, fi->mode);         /* directories/links */
  345.     sz += chown(s, fi->uid, fi->gid);
  346.     ut.actime = fi->atime;             /* fix up times */
  347.     ut.modtime = fi->mtime;
  348.     sz += utime(s, &ut);
  349.     if (sz)
  350.       write_error_log(l);
  351.     if (ret == -4) return ret;
  352.     }
  353.     return (ret == -3) ? -2 : 1;         /* we passed file */
  354. }
  355.